package com.vincent.suchao.lcld;

import java.util.ArrayList;

import com.vincent.suchao.adapter.ContactsAdapter;
import com.vincent.suchao.adapter.NineMenuAdapter;
import com.vincent.suchao.entity.CGAL_Feed;
import com.vincent.suchao.entity.MyContacts;
import com.vincent.suchao.entity.MyContacts.ContactItem;
import com.vincent.suchao.entity.NineMenuItem;
import com.vincent.suchao.util.ImageAsyncDownloader;
import com.vincent.suchao.util.Util;
import com.vincent.suchao.util.ImageAsyncDownloader.LoadBitmapCallback;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class CGAL_Activity extends Activity implements OnClickListener {
	private TextView tv_tittle;
	private ImageAsyncDownloader mImageAsyncDownloader;
	ArrayList<CGAL_Feed> feeds;
	private Button b_back;
	private static final int MSG_LOAD_SUCCESS = 1;
	private static final int MSG_LOAD_FAIL = MSG_LOAD_SUCCESS + 1;
	private Dialog loadingBar;
	private static int index = 0;
	private LinearLayout ll_circleList;
	private MySlide mySlide;
	private LayoutInflater mInflater;
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_LOAD_SUCCESS:
				if (loadingBar != null)
					loadingBar.dismiss();

				if (feeds != null) {
					for (int i = 0; i < feeds.size(); i++) {
						CGAL_Feed f = feeds.get(i);
						View view = mInflater.inflate(R.layout.inflater_cgal1, null);
						final ProgressBar pb_bar = (ProgressBar) view.findViewById(R.id.pb_bar);
						final ImageView iv_cgal = (ImageView) view.findViewById(R.id.iv_cgal);
						final ImageView iv_zoom = (ImageView) view.findViewById(R.id.iv_zoom);
						iv_zoom.setOnClickListener(CGAL_Activity.this);
						iv_zoom.setTag(R.id.iv_zoom, f.getId());
						Bitmap bitmap = mImageAsyncDownloader.loadBitmap(CGAL_Activity.this, f.getThumb(), new LoadBitmapCallback() {
							@Override
							public void imageLoaded(Bitmap bitmap, String imageUrl) {
								iv_cgal.setImageBitmap(bitmap);
								pb_bar.setVisibility(View.GONE);
							}
						},-1, -1);
						if (bitmap != null) {
							iv_cgal.setImageBitmap(bitmap);
							pb_bar.setVisibility(View.GONE);
						}
						mySlide.addView(view);
					}
				}

				if (mySlide.getChildCount() > 0)
					mySlide.setToScreen(0);
				break;
			case MSG_LOAD_FAIL:
				if (loadingBar != null)
					loadingBar.dismiss();
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cgal);
		mImageAsyncDownloader = new ImageAsyncDownloader();
		this.mInflater = LayoutInflater.from(this);
		init();
		getNews();
	}

	private void getNews() {
		loadingBar.show();
		new Thread(new Runnable() {

			@Override
			public void run() {
				String json = Util.testCGAL();
				// System.out.println("json: " + json);
				try {
					String result = Util.post(Util.NEWS_ZXDT_URL, json);
					feeds = Util.parseCGALJSON(result);
					Message msg = mHandler.obtainMessage();
					msg.what = MSG_LOAD_SUCCESS;
					mHandler.sendMessage(msg);
				} catch (Exception e) {
					e.printStackTrace();
					Message msg = mHandler.obtainMessage();
					msg.obj = e.getMessage();
					msg.what = MSG_LOAD_FAIL;
					mHandler.sendMessage(msg);
				}
			}
		}).start();
	}

	private void init() {
		ll_circleList = (LinearLayout) findViewById(R.id.ll_circleList);
		tv_tittle = (TextView) findViewById(R.id.tv_tittle);
		tv_tittle.setText(getString(R.string.menu_com_cgal));
		b_back = (Button) findViewById(R.id.b_back);
		b_back.setOnClickListener(this);
		int screen_width = getWindowManager().getDefaultDisplay().getWidth();
		mySlide = (MySlide) findViewById(R.id.mySlide);
		mySlide.setScreen_width(screen_width);
		mySlide.setsListener(new MySlide.SlideListener() {
			public void onComplete(int index) {
				showPageNum(index);
			}

		});
		if (mySlide.getChildCount() > 0)
			mySlide.setToScreen(0);
		int isFromTabhost = getIntent().getIntExtra(TabHostActivity.TABACTIVITY_TAG, 0);
		if (isFromTabhost == 1)
			b_back.setVisibility(View.GONE);

		loadingBar = Util.progressDialog(this, R.string.loading, R.string.loading, false);
	}

	public void showPageNum(int currentIndex) {
		index = currentIndex;
		ll_circleList.removeAllViews();

		for (int i = 0; i < mySlide.getChildCount(); i++) {
			ImageView iv = new ImageView(this);
			int padding = 10;
			iv.setPadding(padding, padding, padding, padding);
			int param = 31;
			LinearLayout.LayoutParams circleImage_Param = new LinearLayout.LayoutParams(param, param);

			iv.setLayoutParams(circleImage_Param);
			iv.setId(i);
			iv.setOnTouchListener(new View.OnTouchListener() {
				public boolean onTouch(View v, MotionEvent event) {
					if (event.getAction() == MotionEvent.ACTION_DOWN) {

						if (index == v.getId()) {
							return false;
						} else {
							index = v.getId();
							mySlide.setToScreen(index);
						}
					} else if (event.getAction() == MotionEvent.ACTION_UP) {

					}

					return true;
				}
			});
			if (i == index) {
				iv.setImageResource(R.drawable.circel);
			} else {
				iv.setImageResource(R.drawable.circel_null);
			}
			ll_circleList.addView(iv);
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.gsjj, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.b_back:
			finish();
			break;
		case R.id.iv_zoom:
			int id = (Integer) v.getTag(R.id.iv_zoom);
			Intent intent = new Intent();
			intent.putExtra("R.id.iv_zoom", id);
			intent.setClass(this, CGAL_DetailActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}

	}

}
